#ifndef _WMI_CONNECTION_H
#define _WMI_CONNECTION_H

/******************************************************************************
** @file	wmi_connection.h
** @author	Nestor Felipe
** @author	Armando Orellana
** @date	Jul 22, 2010
**
** @desc	This class represent a specific conection "WMI conection".
**
**
******************************************************************************/

#include <iostream>
#include <comdef.h>
#include <Wbemidl.h>
# pragma comment(lib, "wbemuuid.lib")
# pragma comment(lib, "credui.lib")
# pragma comment(lib, "comsuppw.lib")
#include <wincred.h>
#include <strsafe.h>
#include <typeinfo>
#include <cstdio>
#include "wmi_iconnection.h"
#include "wmi_constants.h"
#include <log4cxx\logger.h>
#include <log4cxx\xml\domconfigurator.h>

class WMI_Connection : public WMI_IConnection 
{
public:

	/**
	* Class constructor to create a new WMI conection 
    *
	*/
	WMI_Connection()
		: _pSvc(NULL), _userAcct(NULL), _pLoc(NULL), _pEnumerator(NULL)
	{
	}

    /**
    * Class destructor
    */
	~WMI_Connection()
	{
	}

    /**
    * copy constructor, create a new conection from other conection
    */
	WMI_Connection(const WMI_Connection& newCon)
	{
		_pSvc = newCon._pSvc;
		_pLoc = newCon._pLoc;
		_userAcct = newCon._userAcct;
	}

	/**
    * overwrite of operator= to use de assignation
    */
	WMI_Connection& operator=(const WMI_Connection& newCon)
	{
		_pSvc = newCon._pSvc;
		_pLoc = newCon._pLoc;
		_userAcct = newCon._userAcct;
	}

	
	/**
	* To execute a wql query, if the query success then the result its obtained 
	* whit the member getIEnumWbemClassObject().
	* 
	* @param wql_query	- a query in wstring format
	* @return	integer to represent the success of the query
	*			'0' (zero) if the query success,
	*			other valor represent error, its like a HRESULT of wmi library.
	*/
	WMI_HResult* executeQuery(const wchar_t* wql_query);

	/**
	* Retrieves a class object, only retrieves objects from the namesace
	* associated with the current IWbemServices object.
	* 
	* @param className	-	Path of the object to retrieve, if this is NULL, an empty
	*						object is returned 
	* @param classObject -	if not NULL, this retrieves the object, if it is found.
	*						in all cases error, this parameter is set to point to NULL.
	* @return	integer to represent the success of the member
	*			'0' (zero or NULL) if the query success,
	*			other valor represent error, its a HRESULT valors of wmi library.
	*/
	WMI_HResult* getClassObject(const wchar_t* className, IWbemClassObject** classObject);	
	
	/**
	* member to close the current conection. set free all valor attributes of this class.
	* 
	*/
	virtual void close();

	/**
    * set a IWbemServices 
    *
	* @param pointer IWbemServices.
    */
	void 
	setIWbemServices(IWbemServices* p_svc)
	{
		_pSvc = p_svc;
	}

	/**
    * get the pointer to IWbemServices.
    *
	* @return	IWbemServices.
    */
	IWbemServices* 
	getServices()
	{
		return _pSvc;
	}

	/**
    * set a COAUTHIDENTITY
    *
	* @param acc -	pointer COAUTHIDENTITY.
    */
	void 
	setCoAuthidentity(COAUTHIDENTITY* acc)
	{
		_userAcct = acc;
	}

	/**
    * set a IWbemLocator
    *
	* @param loc -	pointer IWbemLocator.
    */
	void 
	setIWbemLocator(IWbemLocator* loc)
	{
		_pLoc = loc;
	}

	/**
    * get a pointer to IEnumWbemClassObject, the result is stored
	* here after running a query.
    *
	* @return	IEnumWbemClassObject of the result of a query.
    */
	IEnumWbemClassObject* 
	getIEnumWbemClassObject()
	{
		return _pEnumerator;
	}

private:
	
	/**
    * Obtain the initial namespace pointer to the IWbemServices interface
	* for WMI on a specific host computer.
    */
	IWbemLocator* _pLoc;

	/**
    * Used by client and providers to access WMI services.
    */
	IWbemServices* _pSvc;

	/**
    * structure of WMI to represent the user name and password of this conection.
    */
	COAUTHIDENTITY* _userAcct;

	/**
    * to stored the answer, used for WMI to enumerate
	* Common Information Model (CIM) objects.
    */
	IEnumWbemClassObject* _pEnumerator;

};

#endif // WMIConnection_h
